<template>
  <div class="login">
    <div class="login-container">
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        class="login-form"
        auto-complete="on"
        label-position="left"
      >
        <div class="login-title">
          <h3>非遗文化传承后台管理系统</h3>
        </div>

        <el-form-item prop="username">
          <el-input
            class="login-input"
            v-model="form.username"
            placeholder="请输入用户名"
            type="text"
          >
            <i slot="prefix" class="el-input__icon el-icon-user"></i>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            class="login-input"
            v-model="form.password"
            placeholder="请输入密码"
            :type="passwordType ? 'password' : 'text'"
          >
            <i slot="prefix" class="el-input__icon el-icon-lock"></i>
            <i
              slot="suffix"
              class="el-input__icon"
              :class="{
                'el-icon-key': !passwordType,
                'el-icon-view': passwordType,
              }"
              @click="passwordType = !passwordType"
            ></i>
          </el-input>
        </el-form-item>
        <el-button
          :loading="loading"
          type="primary"
          style="width: 100%; margin-bottom: 30px"
          @click.native.prevent="handleLogin"
          >登 录</el-button
        >
      </el-form>
    </div>
  </div>
</template>

<script>
import { Message } from "element-ui";
export default {
  name: "Login",
  data() {
    return {
      form: {
        username: "admin",
        password: "Aa123456",
      },
      rules: {
        username: [{ required: true, trigger: "blur" }],
        password: [{ required: true, trigger: "blur" }],
      },
      loading: false,
      passwordType: true,
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  methods: {
    handleLogin() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/login", this.form)
            .then(() => {
              Message.success("登录成功");
              this.$router.push({ path: this.redirect || "/" });
            })
            .catch((error) => {
              Message.error(error.message);
            })
            .finally(() => {
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss">
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login {
  background-color: $bg;
  min-height: 100%;
  width: 100%;
  &-container {
    overflow: hidden;
  }
  &-form {
    width: 520px;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }
  &-title {
    text-align: center;
    color: $light_gray;
    font-size: 26px;
  }
  &-input {
    display: inline-block;
    height: 47px;
    width: 100%;
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 6px;
    line-height: 47px;
    padding-left: 10px;
    input {
      height: 100%;
      width: 100%;
      background: transparent;
      border: 0px;
      border-radius: 0px;
      color: $light_gray;
    }
  }
}
.el-input__icon {
  font-size: 18px;
  margin-right: 10px;
}
</style>
